package controller;

import constant.Constant;
import util.ComponentUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.URL;

//@Slf4j
public class ViewController extends JFrame {

    SpringLayout springLayout = new SpringLayout();
    JPanel jPanel = new JPanel(springLayout);
    JLabel cycleLabel = new JLabel("生长周期: ");
    JComboBox<String> cycleComboBox = new JComboBox<>(new String[]{"保育", "育肥", "怀孕", "分娩", "公猪"});
    JLabel massLabel = new JLabel("体量(Kg/头): ");
    JSpinner massSpinner = new JSpinner();
    JLabel countLabel = new JLabel("数量(只): ");
    JSpinner countSpinner = new JSpinner();
    JLabel airInletLabel = new JLabel("进风口");
    JLabel indoorLabel = new JLabel("室内");
    JLabel temperatureLeftLabel = new JLabel("温度(℃): ");
    JSpinner temperatureLeftSpinner = new JSpinner();
    JLabel temperatureRightLabel = new JLabel("温度(℃): ");
    JSpinner temperatureRightSpinner = new JSpinner();
    JLabel relativeHumidityLeftLabel = new JLabel("相对湿度(%): ");
    JSpinner relativeHumidityLeftSpinner = new JSpinner();
    JLabel relativeHumidityRightLabel = new JLabel("相对湿度(%): ");
    JSpinner relativeHumidityRightSpinner = new JSpinner();

    JLabel ventilationQuantityLabel = new JLabel("通风量: ");
    JTextField ventilationQuantityResultTextField = new JTextField();

    JButton calculateButton = new JButton("计算");
    JButton resetButton = new JButton("清零");

    SystemTray systemTray;


    public ViewController(){
        super("通风量计算器");
        Container contentPane = getContentPane();
        cycleComboBox.setPreferredSize(ComponentUtil.getCommonTextDimension());
        jPanel.add(cycleLabel);
        jPanel.add(cycleComboBox);

        jPanel.add(massLabel);
//        massSpinner.setValue(10);
        massSpinner.setPreferredSize(ComponentUtil.getCommonTextDimension());
        massSpinner.setFont(ComponentUtil.getCommonTextFont());
        jPanel.add(massSpinner);

        jPanel.add(countLabel);
//        countSpinner.setValue(10);
        countSpinner.setPreferredSize(ComponentUtil.getCommonTextDimension());
        countSpinner.setFont(ComponentUtil.getCommonTextFont());
        jPanel.add(countSpinner);

        airInletLabel.setFont(ComponentUtil.getTitleTextFont());
        jPanel.add(airInletLabel);
        indoorLabel.setFont(ComponentUtil.getTitleTextFont());
        jPanel.add(indoorLabel);

        jPanel.add(temperatureLeftLabel);
        temperatureLeftSpinner.setPreferredSize(ComponentUtil.getCommonTextDimension());
        temperatureLeftSpinner.setFont(ComponentUtil.getCommonTextFont());
        jPanel.add(temperatureLeftSpinner);

        jPanel.add(temperatureRightLabel);
        temperatureRightSpinner.setPreferredSize(ComponentUtil.getCommonTextDimension());
        temperatureRightSpinner.setFont(ComponentUtil.getCommonTextFont());
        jPanel.add(temperatureRightSpinner);

        jPanel.add(relativeHumidityLeftLabel);
        relativeHumidityLeftSpinner.setPreferredSize(ComponentUtil.getCommonTextDimension());
        relativeHumidityLeftSpinner.setFont(ComponentUtil.getCommonTextFont());
        jPanel.add(relativeHumidityLeftSpinner);


        jPanel.add(relativeHumidityRightLabel);
        relativeHumidityRightSpinner.setPreferredSize(ComponentUtil.getCommonTextDimension());
        relativeHumidityRightSpinner.setFont(ComponentUtil.getCommonTextFont());
        jPanel.add(relativeHumidityRightSpinner);

        jPanel.add(ventilationQuantityLabel);
        ventilationQuantityResultTextField.setPreferredSize(ComponentUtil.getCommonTextDimension());
        jPanel.add(ventilationQuantityResultTextField);

        jPanel.add(calculateButton);
        jPanel.add(resetButton);

        contentPane.add(jPanel);

        /**
         * 设置约束的第一种写法，相对复杂，需要计算
         */
        SpringLayout.Constraints cycleLabelConstraints = springLayout.getConstraints(cycleLabel);
        // edgeName:东南西北 s:值
        Spring cycleLabelConstraintValueNorth = cycleLabelConstraints.getConstraint(SpringLayout.NORTH);
        cycleLabelConstraints.setConstraint(SpringLayout.WEST, Spring.constant(Constant.PADDING_10));
        cycleLabelConstraints.setConstraint(SpringLayout.NORTH, Spring.sum(cycleLabelConstraintValueNorth, Spring.constant(Constant.PADDING_10)));
        /**
         * 设置约束的第二种方法，相对简单
         * e1：要设置组件的哪个边界
         * c1：要设置的组件
         * pad：距离值
         * e2：参照物的边界值
         * c2：参照物
         * 以下是设置作者label
         */
        springLayout.putConstraint(SpringLayout.WEST, cycleComboBox, Constant.PADDING_20, SpringLayout.EAST, cycleLabel);
        springLayout.putConstraint(SpringLayout.NORTH, cycleComboBox, Constant.ZERO_PADDING, SpringLayout.NORTH, cycleLabel);

        springLayout.putConstraint(SpringLayout.WEST, massLabel, Constant.ZERO_PADDING, SpringLayout.WEST, cycleLabel);
        springLayout.putConstraint(SpringLayout.NORTH, massLabel, Constant.PADDING_20, SpringLayout.SOUTH, cycleLabel);

        springLayout.putConstraint(SpringLayout.WEST, massSpinner, Constant.PADDING_20, SpringLayout.EAST, massLabel);
        springLayout.putConstraint(SpringLayout.NORTH, massSpinner, Constant.ZERO_PADDING, SpringLayout.NORTH, massLabel);

        springLayout.putConstraint(SpringLayout.WEST, countLabel, Constant.ZERO_PADDING, SpringLayout.WEST, massLabel);
        springLayout.putConstraint(SpringLayout.NORTH, countLabel, Constant.PADDING_20, SpringLayout.SOUTH, massLabel);

        springLayout.putConstraint(SpringLayout.WEST, countSpinner, Constant.PADDING_20, SpringLayout.EAST, countLabel);
        springLayout.putConstraint(SpringLayout.NORTH, countSpinner, Constant.ZERO_PADDING, SpringLayout.NORTH, countLabel);


        springLayout.putConstraint(SpringLayout.NORTH, airInletLabel, Constant.PADDING_60, SpringLayout.SOUTH, countLabel);
        springLayout.putConstraint(SpringLayout.WEST, airInletLabel, Constant.ZERO_PADDING, SpringLayout.WEST, countLabel);

        springLayout.putConstraint(SpringLayout.NORTH, indoorLabel, Constant.ZERO_PADDING, SpringLayout.NORTH, airInletLabel);
        springLayout.putConstraint(SpringLayout.WEST, indoorLabel, (Constant.PADDING_60 * 7 / 2), SpringLayout.EAST, airInletLabel);

        springLayout.putConstraint(SpringLayout.NORTH, temperatureLeftLabel, Constant.PADDING_60, SpringLayout.SOUTH, airInletLabel);
        springLayout.putConstraint(SpringLayout.WEST, temperatureLeftLabel, Constant.ZERO_PADDING, SpringLayout.WEST, airInletLabel);

        springLayout.putConstraint(SpringLayout.NORTH, temperatureLeftSpinner, Constant.ZERO_PADDING, SpringLayout.NORTH, temperatureLeftLabel);
        springLayout.putConstraint(SpringLayout.WEST, temperatureLeftSpinner, Constant.PADDING_20, SpringLayout.EAST, temperatureLeftLabel);

        springLayout.putConstraint(SpringLayout.NORTH, temperatureRightLabel, Constant.PADDING_60, SpringLayout.SOUTH, indoorLabel);
        springLayout.putConstraint(SpringLayout.WEST, temperatureRightLabel, Constant.ZERO_PADDING, SpringLayout.WEST, indoorLabel);

        springLayout.putConstraint(SpringLayout.NORTH, temperatureRightSpinner, Constant.ZERO_PADDING, SpringLayout.NORTH, temperatureRightLabel);
        springLayout.putConstraint(SpringLayout.WEST, temperatureRightSpinner, Constant.PADDING_20, SpringLayout.EAST, temperatureRightLabel);

        springLayout.putConstraint(SpringLayout.NORTH, relativeHumidityLeftLabel, Constant.PADDING_20, SpringLayout.SOUTH, temperatureLeftLabel);
        springLayout.putConstraint(SpringLayout.WEST, relativeHumidityLeftLabel, Constant.ZERO_PADDING, SpringLayout.WEST, temperatureLeftLabel);

        springLayout.putConstraint(SpringLayout.NORTH, relativeHumidityLeftSpinner, Constant.ZERO_PADDING, SpringLayout.NORTH, relativeHumidityLeftLabel);
        springLayout.putConstraint(SpringLayout.WEST, relativeHumidityLeftSpinner, Constant.PADDING_20, SpringLayout.EAST, relativeHumidityLeftLabel);

        springLayout.putConstraint(SpringLayout.NORTH, relativeHumidityRightLabel, Constant.PADDING_20, SpringLayout.SOUTH, temperatureRightLabel);
        springLayout.putConstraint(SpringLayout.WEST, relativeHumidityRightLabel, Constant.ZERO_PADDING, SpringLayout.WEST, temperatureRightLabel);

        springLayout.putConstraint(SpringLayout.NORTH, relativeHumidityRightSpinner, Constant.ZERO_PADDING, SpringLayout.NORTH, relativeHumidityRightLabel);
        springLayout.putConstraint(SpringLayout.WEST, relativeHumidityRightSpinner, Constant.PADDING_20, SpringLayout.EAST, relativeHumidityRightLabel);

        springLayout.putConstraint(SpringLayout.NORTH, ventilationQuantityLabel, Constant.PADDING_60, SpringLayout.SOUTH, relativeHumidityLeftLabel);
        springLayout.putConstraint(SpringLayout.WEST, ventilationQuantityLabel, Constant.ZERO_PADDING, SpringLayout.WEST, relativeHumidityLeftLabel);

        springLayout.putConstraint(SpringLayout.NORTH, ventilationQuantityResultTextField, Constant.ZERO_PADDING, SpringLayout.NORTH, ventilationQuantityLabel);
        springLayout.putConstraint(SpringLayout.WEST, ventilationQuantityResultTextField, Constant.PADDING_20, SpringLayout.EAST, ventilationQuantityLabel);

        springLayout.putConstraint(SpringLayout.NORTH, calculateButton, Constant.PADDING_60, SpringLayout.SOUTH, ventilationQuantityLabel);
        springLayout.putConstraint(SpringLayout.WEST, calculateButton, Constant.PADDING_60 * 2, SpringLayout.WEST, ventilationQuantityLabel);

        springLayout.putConstraint(SpringLayout.NORTH, resetButton, Constant.ZERO_PADDING, SpringLayout.NORTH, calculateButton);
        springLayout.putConstraint(SpringLayout.WEST, resetButton, Constant.PADDING_60 * 2, SpringLayout.EAST, calculateButton);



        if (SystemTray.isSupported()){
            systemTray = SystemTray.getSystemTray();
            ClassLoader classLoader = ViewController.class.getClassLoader();
            URL logoUrl = classLoader.getResource("file/wind.png");
            TrayIcon trayIcon = new TrayIcon(new ImageIcon(logoUrl).getImage());
            trayIcon.setImageAutoSize(true);
            try {
                systemTray.add(trayIcon);
            } catch (AWTException e) {
                e.printStackTrace();
            }
            this.addWindowListener(new WindowAdapter() {
                @Override
                public void windowIconified(WindowEvent e) {
                    ViewController.this.dispose();
                }
            });
            trayIcon.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    int clickCount = e.getClickCount();
                    if (clickCount == 1){
                        ViewController.this.setExtendedState(JFrame.NORMAL);
                    }
                    ViewController.this.setVisible(true);
                }
            });

        }

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int offsetX = (screenSize.width - 600) / 2;
        int offsetY = (screenSize.height - 400) / 2;
        setLocation(offsetX, offsetY);

        setSize(600, 600);
        setVisible(true);
        setResizable(true);
    }

    public static void main(String[] args) {
        new ViewController();
//        System.out.println(1);
    }
}
